package com.salon.modules.job.controller;

import com.salon.common.annotation.SysLog;
import com.salon.common.http.response.ObjectResult;
import com.salon.common.http.response.PageResult;
import com.salon.common.validator.ValidatorUtils;
import com.salon.modules.job.entity.ScheduleJobEntity;
import com.salon.modules.job.service.ScheduleJobService;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 定时任务
 */
@RestController
@RequestMapping("/sys/schedule")
public class ScheduleJobController {

    @Resource
    private ScheduleJobService scheduleJobService;

    /**
     * 定时任务列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:schedule:select")
    public PageResult<ScheduleJobEntity> list(@ApiIgnore @RequestParam Map<String, Object> params) {
        return scheduleJobService.listByPage(params);
    }

    /**
     * 定时任务信息
     */
    @RequestMapping("/info/{jobId}")
    @RequiresPermissions("sys:schedule:select")
    public ObjectResult<ScheduleJobEntity> info(@PathVariable("jobId") Long jobId) {
        ScheduleJobEntity schedule = scheduleJobService.getById(jobId);
        return ObjectResult.success(schedule);
    }

    /**
     * 保存定时任务
     */
    @SysLog("保存定时任务")
    @RequestMapping("/save")
    @RequiresPermissions("sys:schedule:save")
    public ObjectResult<String> save(@RequestBody ScheduleJobEntity scheduleJob) {
        ValidatorUtils.validateEntity(scheduleJob);
        scheduleJobService.saveJob(scheduleJob);
        return ObjectResult.success();
    }

    /**
     * 修改定时任务
     */
    @SysLog("修改定时任务")
    @RequestMapping("/update")
    @RequiresPermissions("sys:schedule:update")
    public ObjectResult<String> update(@RequestBody ScheduleJobEntity scheduleJob) {
        ValidatorUtils.validateEntity(scheduleJob);
        scheduleJobService.update(scheduleJob);
        return ObjectResult.success();
    }

    /**
     * 删除定时任务
     */
    @SysLog("删除定时任务")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:schedule:delete")
    public ObjectResult<String> delete(@RequestBody Long[] jobIds) {
        scheduleJobService.deleteBatch(jobIds);
        return ObjectResult.success();
    }

    /**
     * 立即执行任务
     */
    @SysLog("立即执行任务")
    @RequestMapping("/run")
    @RequiresPermissions("sys:schedule:update")
    public ObjectResult<String> run(@RequestBody Long[] jobIds) {
        scheduleJobService.run(jobIds);
        return ObjectResult.success();
    }

    /**
     * 暂停定时任务
     */
    @SysLog("暂停定时任务")
    @RequestMapping("/pause")
    @RequiresPermissions("sys:schedule:update")
    public ObjectResult<String> pause(@RequestBody Long[] jobIds) {
        scheduleJobService.pause(jobIds);
        return ObjectResult.success();
    }

    /**
     * 恢复定时任务
     */
    @SysLog("恢复定时任务")
    @RequestMapping("/resume")
    @RequiresPermissions("sys:schedule:update")
    public ObjectResult<String> resume(@RequestBody Long[] jobIds) {
        scheduleJobService.resume(jobIds);
        return ObjectResult.success();
    }

}
